#include "mywidget.h"
#include "ui_mywidget.h"
#include <unistd.h>
#include <QTimer>
#include <stdlib.h>
#include <QThread>
#include <QImage>
#include <QFont>

myWidget::myWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::myWidget)
{
    ui->setupUi(this);
    Read_Serial_Port_Info();
    QTimer *timer = new QTimer();
    //    timer->setInterval(1000);
    timer->start(50);

    file=new QFile(this);

    timer1 = new QTimer();
    //    timer->setInterval(1000);

    QString filename("./lancer.jpg");
    QImage* img=new QImage;
    img->load(filename);

//    ui->label_7->setPixmap(QPixmap::fromImage(*img));

//    ui->widget1->hide();
    ui->widget2->hide();
     ui->widget2->hide();
    ui->lineEdit->setText("1000");
    //    ui->open_Button->setEnabled(0);
    ui->checkBox_3->setChecked(1);
    ui->checkBox_2->setChecked(1);
    connect(timer,SIGNAL(timeout()),this,SLOT(on_timeout()));

}

myWidget::~myWidget()
{
    delete ui;
    this->serial.close();
}

void myWidget::on_pushButton_clicked()
{
    /*保证当前程序只开启一个串口*/
    if(this->serial.isOpen()){
        this->serial.close();
        ui->pushButton->setText("打开串口");
        ui->comboBox->setEnabled(1);
        return ;
        this->timer->stop();
        disconnect(timer,SIGNAL(timeout()),this,SLOT(on_timeout()));\
        disconnect(&serial,SIGNAL(readyRead()),this,SLOT(serialPort_readyRead()));
        free(&this->serial);
    }
    //ui->comboBox->addItem("nihao");
    this->serial.setPortName(ui->comboBox->currentText());
    qDebug()<<ui->comboBox->currentText();
    this->serial.setPortName(ui->comboBox->currentText());
    switch (ui->comboBox_2->currentIndex()) {

    case 1:this->serial.setBaudRate(QSerialPort::Baud115200);
        break;
    case 0:this->serial.setBaudRate(QSerialPort::Baud9600);
        break;
    case 2:this->serial.setBaudRate(QSerialPort::Baud19200);
        break;
    case 3:this->serial.setBaudRate(QSerialPort::Baud38400);
        break;
    case 4:this->serial.setBaudRate(QSerialPort::Baud57600);
        break;
    default:
        break;
    }
    /*this->serial.setBaudRate(QSerialPort::Baud115200);*/
    switch (ui->comboBox_3->currentIndex()) {
    case 0:this->serial.setDataBits(QSerialPort::Data8);
        break;
    case 1:this->serial.setDataBits(QSerialPort::Data5);
        break;
    case 2:this->serial.setDataBits(QSerialPort::Data6);
        break;
    case 3:this->serial.setDataBits(QSerialPort::Data7);
        break;
    default:
        break;
    }
    /*this->serial.setDataBits(QSerialPort::Data8);*/
    switch (ui->comboBox_4->currentIndex()) {
    case 0:this->serial.setParity(QSerialPort::NoParity);
        break;
    case 1:this->serial.setParity(QSerialPort::OddParity);
        break;
    case 2:this->serial.setParity(QSerialPort::SpaceParity);
        break;
    case 3:this->serial.setParity(QSerialPort::MarkParity);
        break;
    default:
        break;

    }/*
    this->serial.setParity(QSerialPort::NoParity);*/
    switch (ui->comboBox_5->currentIndex()) {
    case 0:this->serial.setStopBits(QSerialPort::OneStop);
        break;
    case 1:this->serial.setStopBits(QSerialPort::OneAndHalfStop);
        break;
    case 2:this->serial.setStopBits(QSerialPort::TwoStop);
        break;
    default:
        break;
    }
    /*this->serial.setStopBits(QSerialPort::OneStop);*/
    switch (ui->comboBox_6->currentIndex()) {
    case 0:this->serial.setFlowControl(QSerialPort::NoFlowControl);
        break;
    case 1:this->serial.setFlowControl(QSerialPort::HardwareControl);
        break;
    case 2:this->serial.setFlowControl(QSerialPort::SoftwareControl);
        break;
    default:
        break;
    }
    //    this->serial.setFlowControl(QSerialPort::NoFlowControl);

    if((ui->pushButton->text()=="打开串口")&&!(this->serial.isOpen())){
        if(this->serial.open(QSerialPort::ReadWrite)){
            qDebug()<<"open succ";
            connect(&serial,SIGNAL(readyRead()),this,SLOT(serialPort_readyRead()));
            ui->pushButton->setText("关闭串口");
            ui->comboBox->setEnabled(0);
            //            this->timer->start(50);
        }
    }
}

//自动识别出电脑可用的串口
void myWidget::Read_Serial_Port_Info(void)
{
    int count=0;
    qDebug() <<"1111";
    QString Read_Serial_Port_Info_Buff;
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
    {
        qDebug() << "Name : " << info.portName();
        qDebug() << "Description :"<<info.description();
        ui->comboBox->addItem(info.portName());
        count++;
    }
    qDebug()<<count;
}

void myWidget::on_pushButton_11_clicked()
{
    ui->textBrowser->clear();
}

void myWidget::on_pushButton_2_clicked()
{
    qDebug()<<"send data";
    QString str=ui->textEdit->toPlainText();
    if(ui->checkBox_2->checkState()!=0){
        str+="\n";
    }
    qDebug()<<str;
    //QString 转 QByteArray
    QByteArray bytes = str.toUtf8();
    this->serial.write(bytes);
    this->serial.waitForBytesWritten(10);
}
void myWidget::serialPort_readyRead(){

    QFont ft;
    ft.setPointSize(10);
    ft.setBold(1);
    ui->textBrowser->setFont(ft);

    qDebug()<<"serialPort_readyRead....";
    QByteArray buffer = serial.readAll();
    if(!strcmp(buffer.data(),"")){
        return ;
    }
    if(ui->checkBox->checkState()){
        buffer=buffer.toHex();
    }
    QString str;
    if(ui->checkBox_3->checkState()!=0){
        str="["+QDateTime::currentDateTime().toString("yyyy:MM:dd hh:mm:ss zzz")+"] "+buffer;
    }else{
        str=buffer;
    }

    ui->textBrowser->insertPlainText("\n"+str);

    if(ui->checkBox_4->checkState()){
        QString temp=QDateTime::currentDateTime().toString("./yyyy-MM-dd")+".txt";
        this->file->setFileName(temp);
        file->open(QIODevice::WriteOnly| QIODevice::Append);
        file->write(str.toUtf8()+"\r\n");
        file->close();
    }
    qDebug()<<buffer;
    buffer.clear();
    str.clear();
}

void myWidget::on_timeout(){
    if(!this->serial.isOpen()){
        return ;
    }
    if(this->serial.waitForReadyRead(10)){
        return ;
    }else{
        usleep(10*1000);
    }
}

void myWidget::on_checkBox_3_stateChanged(int arg1)
{
    qDebug()<<"arg1:"<<arg1;
    qDebug()<<ui->checkBox_3->checkState();
}
/*自动滚动*/
void myWidget::on_textBrowser_cursorPositionChanged()
{
    QTextCursor cursor =  ui->textBrowser->textCursor();
    cursor.movePosition(QTextCursor::End);
    ui->textBrowser->setTextCursor(cursor);
}

void myWidget::on_textBrowser_textChanged()
{
    QTextCursor cursor =  ui->textBrowser->textCursor();
    cursor.movePosition(QTextCursor::End);
    ui->textBrowser->setTextCursor(cursor);
}

void myWidget::on_checkBox_stateChanged(int arg1)
{

}

void myWidget::on_checkBox_5_stateChanged(int arg1)
{
    qDebug()<<this->serial.isOpen();
    if((ui->textEdit->toPlainText().compare("")==0) || (this->serial.isOpen()==false)){
        ui->checkBox_5->setCheckState(Qt::Unchecked);
        qDebug()<<"text is empty or serial is not open";
        return ;
    }

    if((ui->checkBox_5->checkState()) && (ui->textEdit->toPlainText().compare(""))){
        long time_ms=ui->lineEdit->text().toLong();
        this->timer1->start(time_ms);
        connect(timer1,SIGNAL(timeout()),this,SLOT(on_time1_timeout()));
        ui->lineEdit->setEnabled(0);
    }else{
        ui->lineEdit->setEnabled(1);
        disconnect(timer1,SIGNAL(timeout()),this,SLOT(on_time1_timeout()));
        this->timer1->stop();
    }
}
void myWidget::on_time1_timeout(){
    qDebug()<<"timer send data";
    QString str=ui->textEdit->toPlainText();
    if(ui->checkBox_2->checkState()!=0){
        str+="\n";
    }
    qDebug()<<str;
    //QString 转 QByteArray
    QByteArray bytes = str.toUtf8();
    this->serial.write(bytes);
    this->serial.waitForBytesWritten(10);
}

void myWidget::on_open_Button_clicked()
{
//    ui->label_7->hide();
    //ui->label_7->setEnabled(0);
    //ui->open_Button->setEnabled(0);
//    ui->open_Button->hide();
    return ;
}

void myWidget::on_pushButton_3_clicked()
{
    qDebug()<<"send data";
    QString str=ui->lineEdit_4->text();
    if(ui->checkBox_2->checkState()!=0){
        str+="\n";
    }
    qDebug()<<str;
    //QString 转 QByteArray
    QByteArray bytes = str.toUtf8();
    this->serial.write(bytes);
    this->serial.waitForBytesWritten(10);
}

void myWidget::on_pushButton_4_clicked()
{
    qDebug()<<"send data";
    QString str=ui->lineEdit_5->text();
    if(ui->checkBox_2->checkState()!=0){
        str+="\n";
    }
    qDebug()<<str;
    //QString 转 QByteArray
    QByteArray bytes = str.toUtf8();
    this->serial.write(bytes);
    this->serial.waitForBytesWritten(10);
}

void myWidget::on_pushButton_5_clicked()
{
    qDebug()<<"send data";
    QString str=ui->lineEdit_6->text();
    if(ui->checkBox_2->checkState()!=0){
        str+="\n";
    }
    qDebug()<<str;
    //QString 转 QByteArray
    QByteArray bytes = str.toUtf8();
    this->serial.write(bytes);
    this->serial.waitForBytesWritten(10);
}


void myWidget::on_pushButton_6_clicked()
{
    qDebug()<<"send data";
    QString str=ui->lineEdit_7->text();
    if(ui->checkBox_2->checkState()!=0){
        str+="\n";
    }
    qDebug()<<str;
    //QString 转 QByteArray
    QByteArray bytes = str.toUtf8();
    this->serial.write(bytes);
    this->serial.waitForBytesWritten(10);
}

void myWidget::on_pushButton_7_clicked()
{
    qDebug()<<"send data";
    QString str=ui->lineEdit_8->text();
    if(ui->checkBox_2->checkState()!=0){
        str+="\n";
    }
    qDebug()<<str;
    //QString 转 QByteArray
    QByteArray bytes = str.toUtf8();
    this->serial.write(bytes);
    this->serial.waitForBytesWritten(10);
}

void myWidget::on_pushButton_8_clicked()
{
    qDebug()<<"send data";
    QString str=ui->lineEdit_9->text();
    if(ui->checkBox_2->checkState()!=0){
        str+="\n";
    }
    qDebug()<<str;
    //QString 转 QByteArray
    QByteArray bytes = str.toUtf8();
    this->serial.write(bytes);
    this->serial.waitForBytesWritten(10);
}

void myWidget::on_pushButton_9_clicked()
{
    qDebug()<<"send data";
    QString str=ui->lineEdit_10->text();
    if(ui->checkBox_2->checkState()!=0){
        str+="\n";
    }
    qDebug()<<str;
    //QString 转 QByteArray
    QByteArray bytes = str.toUtf8();
    this->serial.write(bytes);
    this->serial.waitForBytesWritten(10);
}

void myWidget::on_pushButton_10_clicked()
{
    qDebug()<<"send data";
    QString str=ui->lineEdit_11->text();
    if(ui->checkBox_2->checkState()!=0){
        str+="\n";
    }
    qDebug()<<str;
    //QString 转 QByteArray
    QByteArray bytes = str.toUtf8();
    this->serial.write(bytes);
    this->serial.waitForBytesWritten(10);
}

void myWidget::on_pushButton_12_clicked()
{
    qDebug()<<"send data";
    QString str=ui->lineEdit_12->text();
    if(ui->checkBox_2->checkState()!=0){
        str+="\n";
    }
    qDebug()<<str;
    //QString 转 QByteArray
    QByteArray bytes = str.toUtf8();
    this->serial.write(bytes);
    this->serial.waitForBytesWritten(10);
}

void myWidget::on_pushButton_13_clicked()
{
    qDebug()<<"send data";
    QString str=ui->lineEdit_13->text();
    if(ui->checkBox_2->checkState()!=0){
        str+="\n";
    }
    qDebug()<<str;
    //QString 转 QByteArray
    QByteArray bytes = str.toUtf8();
    this->serial.write(bytes);
    this->serial.waitForBytesWritten(10);
}
